//index.js
//获取应用实例
const common = require('../../utils/util.js')
const app = getApp()

Page({
    data: {
        nowDate: '',
        userInfo: null,
        userCode: '',
        userType: null,
        belongRefer: null,
        showAdv: true,
        popImg: '',
        realyImg: '',
        entranceImg: '',
        showShareImg: false,
        shareImg: [],
        previewImg: '',
        imgSwiper: [],
        hotShop: [],
        shopExclusiveGoods: [],
        activityGoods: [],
        upgradeGoods: [],
        newGoods: [],
        hotSaleGoods: [],
        hotClickGoods: [],
        zeroBuy: [],
        zeroBuyList: [],
        indexTypeClass: [],
        indexClassList: [],
        guessLikeClass: [],
        guessLikeList: [],
        guessType: '',
        nowPage: 0,
        isLoading: false,
        isAll: false
    },
    onLoad: function(options) {
        if (options.scene) {
            let sceneArr = options.scene.split(',')
            wx.setStorageSync('userShare', sceneArr[0])
            if (wx.getStorageSync('token')) {
                let data = {
                    belong_refer: sceneArr[0]
                }
                common.post('User/set_belong_refer', data).then(function(res) {}).catch(function(e) {})
            }
        }
        if (options.userShare) {
            wx.setStorageSync('userShare', options.userShare)
            if (wx.getStorageSync('token')) {
                let data = {
                    belong_refer: options.userShare
                }
                common.post('User/set_belong_refer', data).then(function(res) {}).catch(function(e) {})
            }
        }
        if (wx.getStorageSync('roleType')) {
            this.setData({
                userType: wx.getStorageSync('roleType')
            })
        }
        this.setData({
            nowDate: new Date().getTime()
        })
        if (wx.getStorageSync('token')) {
            this.getUserInfo()
        }
        this.getPopImg()
        this.getRealyImg()
        this.getEntranceImg()
        this.getIndexData()
        this.getHotShop()
        this.getShopExclusiveGoods()
        // this.getActivityGoods()
        this.getUpgradeGoods()
        this.getNewGoods()
        this.getHotSaleGoods()
        this.getHotClickGoods()
        this.getZeroBuy()
        this.getShareImg()
    },
    onHide: function() {
        this.setData({
            isLoading: false
        })
    },
    onShow: function() {

    },
    onPullDownRefresh: function() {
        this.setData({
            guessLikeList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 1000)
    },

    onReachBottom: function() {
        this.getGuessLikeList()
    },

    onShareAppMessage: function(res) {
        let userId = wx.getStorageSync('userId')
        let relayImg = this.data.realyImg
        if (userId) {
            return {
                title: '成为会员，更多优质爆品等你来领！',
                path: '/pages/index/index?userShare=' + userId,
                imageUrl: relayImg
            }
        } else {
            return {
                title: '成为会员，更多优质爆品等你来领！',
                path: '/pages/index/index',
                imageUrl: relayImg
            }
        }
    },

    closeAdv: function() {
        this.setData({
            showAdv: false
        })
        wx.showTabBar()
    },

    getUserInfo: function() {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
            }
        }).catch(function(e) {})
    },

    getUserCode: function() {
        const that = this
        let data = {}
        common.post('QrCodes/app_create_qr', data).then(function(res) {
            that.setData({
                userCode: res
            })
        }).catch(function(e) {})
    },

    getShareImg: function() {
        let that = this;
        common.post("Upimg/get_app_img", {
            activity: 785,
            type: 1
        }).then(function(res) {
            that.setData({
                shareImg: res,
                previewImg: res[0]
            })
        }).catch(function(e) {})
    },

    getPopImg: function() {
        const that = this
        let data = {
            activity: 785,
            images_id: 9
        }
        common.post('Images/get_img', data).then(function(res) {
            that.setData({
                popImg: res[0]
            })
        }).catch(function(e) {})
    },

    getRealyImg: function() {
        const that = this
        let data = {
            activity: 785,
            images_id: 11
        }
        common.post('Images/get_img', data).then(function(res) {
            that.setData({
                realyImg: res[0].images
            })
        }).catch(function(e) {})
    },

    getEntranceImg: function() {
        const that = this
        let data = {
            activity: 785,
            images_id: 12
        }
        common.post('Images/get_img', data).then(function(res) {
            that.setData({
                entranceImg: res[0].images
            })
        }).catch(function(e) {})
    },

    getIndexData: function() {
        const that = this
        let data = {
            activity: 785,
        }
        common.post('Home/index', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    imgSwiper: res.slide,
                    indexTypeClass: res.recommen_class,
                    guessLikeClass: res.all_class
                })
                that.getClassList()
                that.getGuessLikeList()
            }
        }).catch(function(e) {})
    },

    getClassList: function() {
        const that = this
        let indexClassList = []
        common.post('shelf/get_shelf', {
            activity: 785
        }).then(function(res) {
            if (res.code == 204) {
                indexClassList = []
            } else {
                indexClassList = res
            }
            that.setData({
                indexClassList: indexClassList
            })
        }).catch(function(e) {})
    },

    getGuessLikeList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let guessLikeList = this.data.guessLikeList
        let data = {
            activity: 785,
            is_goods: "0",
            page: this.data.nowPage,
            limit: 10,
            class_id: this.data.guessType
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    guessLikeList: guessLikeList,
                    isAll: true
                })
            } else {
                guessLikeList = guessLikeList.concat(res)
                that.setData({
                    guessLikeList: guessLikeList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    getHotShop: function() {
        const that = this
        let data = {
            activity: 785,
            recommend: 1,
            page: 0,
            limit: 3
        }
        common.post('Store/store_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                hotShop: res
            })
        }).catch(function(e) {})
    },

    getShopExclusiveGoods: function() {
        const that = this
        let data = {
            activity: 785,
            is_goods: 8,
            page: 0,
            limit: 999
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                shopExclusiveGoods: res
            })
        }).catch(function(e) {})
    },

    getActivityGoods: function() {
        const that = this
        let data = {
            activity: 785,
            type: 4
        }
        common.post('shelf/get_shelf', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    activityGoods: []
                })
            } else {
                that.setData({
                    activityGoods: res
                })
            }
        }).catch(function(e) {})
    },

    getUpgradeGoods: function() {
        const that = this
        let data = {
            activity: 785,
            type: 3
        }
        common.post('shelf/get_shelf', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    upgradeGoods: []
                })
            } else {
                that.setData({
                    upgradeGoods: res
                })
            }
        }).catch(function(e) {})
    },

    getNewGoods: function() {
        const that = this
        let data = {
            activity: 785,
            order_by: 'add_time',
            asc: 2,
            page: 0,
            limit: 20
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    newGoods: []
                })
            } else {
                that.setData({
                    newGoods: res
                })
            }
        }).catch(function(e) {})
    },

    getHotSaleGoods: function() {
        const that = this
        let data = {
            activity: 785,
            order_by: 'sold_num',
            asc: 2,
            page: 0,
            limit: 20
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                hotSaleGoods: res
            })
        }).catch(function(e) {})
    },

    getHotClickGoods: function() {
        const that = this
        let data = {
            activity: 785,
            order_by: 'click',
            asc: 2,
            page: 0,
            limit: 20
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                hotClickGoods: res
            })
        }).catch(function(e) {})
    },

    guessClick: function(e) {
        if (this.data.guessType == e.detail) {
            return
        }
        this.setData({
            guessType: e.detail,
            nowPage: 0,
            isAll: false,
            isLoading: false
        })
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let guessLikeList = this.data.guessLikeList
        let data = {
            activity: 785,
            is_goods: "0",
            page: this.data.nowPage,
            limit: 10,
            class_id: this.data.guessType
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                guessLikeList = []
                that.setData({
                    guessLikeList: guessLikeList,
                    isAll: true
                })
            } else {
                guessLikeList = []
                guessLikeList = guessLikeList.concat(res)
                that.setData({
                    guessLikeList: guessLikeList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    getZeroBuy: function() {
        const that = this
        let data = {
            activity: 785,
            type: 6
        }
        common.post('Shelf/get_shelf', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    zeroBuy: []
                })
            } else {
                let zeroBuyList = that.data.zeroBuyList
                that.setData({
                    zeroBuy: res
                })
                that.data.zeroBuy.forEach((item, i) => {
                    let data = {
                        activity: 785,
                        shelf_id: item.id
                    }
                    common.post('Activity/get_activity_goods', data).then(function(res) {
                        if (res.code == 204) {
                            zeroBuyList[i] = []
                        } else {
                            zeroBuyList[i] = res
                        }
                        that.setData({
                            zeroBuyList: zeroBuyList
                        })
                    }).catch(function(e) {})
                })
            }
        }).catch(function(e) {})
    },

    goTeach: function() {
        wx.navigateTo({
            url: '/pages/h5/h5?page=index_5'
        })
    },

    goShopApply: function() {
        wx.navigateTo({
            url: '/pages/shopManager/shopBuy/shopBuy'
        })
    },

    goShopApplyCourse: function() {
        wx.navigateTo({
            url: '/pages/shopManager/shopApplyCourse/shopApplyCourse'
        })
    },

    goActivity: function(){
        wx.navigateTo({
            url: '/pages/h5/h5?page=index_7'
        })
    }
})